﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
//using Microsoft.Office.Interop.Excel;
using DevExpress.Spreadsheet.Functions;
using DevExpress.Spreadsheet;
using System.IO;
//using DevExpress.XtraSpreadsheet.Model;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmBAPreview : Form
    {
        
        GridView GV = null;
        public FrmBAPreview(GridView _GV)
        {
            InitializeComponent();
            this.GV = _GV;           
        }
        public void LoadData()
        {
            MemoryStream sstream = new MemoryStream();
            DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
            xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
            xlsOption.ShowGridLines = false;
            xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;
            GV.ExportToXls(sstream, xlsOption);
            spreadsheet_TrimList.LoadDocument(sstream, DocumentFormat.Xls);
            spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(1, 100);
            spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Rows.AutoFit(1, 100);            
            for (int i = 0; i < 12; i++)
            {
                spreadsheet_TrimList.ActiveWorksheet.Rows.Insert(i, 1);
            }
            spreadsheet_TrimList.ActiveWorksheet.Shapes.AddPicture(@"D:\Projects\COMO\Image\logo.png", 50, 50, 1200, 300, false);
            spreadsheet_TrimList.ActiveWorksheet.Rows[7][4].SetValue("BALANCE DETAILS");
            spreadsheet_TrimList.ActiveWorksheet.Rows[7][4].Font.UnderlineType = UnderlineType.Single;
            spreadsheet_TrimList.ActiveWorksheet.Rows[7][4].Font.Bold = true;
            spreadsheet_TrimList.ActiveWorksheet.Rows[7][4].Font.Color = Color.Blue;
            spreadsheet_TrimList.ActiveWorksheet.Rows[7][4].Font.Size = 18;
            spreadsheet_TrimList.ActiveWorksheet.Rows[8][4].SetValue("          Export Date: " + DateTime.Now.ToShortDateString());
            spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Rows[12].Height = 100;
            spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Rows[13].Height = 100;
            spreadsheet_TrimList.Document.Worksheets.ActiveWorksheet.Columns[0].Width = 300;
        }

        private void dataNavigator_TrimList_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Print")
            {
                spreadsheet_TrimList.ShowPrintDialog();
            }
            else
            {
                String fileName = null;
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                sfd.Title = "Save an Excel File";
                sfd.FileName = "Balance"+ String.Format("{0:MMMddyyyy}", DateTime.Now);
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        fileName = sfd.FileName;
                        spreadsheet_TrimList.SaveDocument(fileName, DocumentFormat.Xls);
                        DialogResult dlr = MessageBox.Show("Open File Export", "Do you want to open it?",
                                           MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dlr == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(fileName);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
}
